//
//  IndividualViewController.m
//  MM
//
//  Created by 王晨辉 on 15/5/18.
//  Copyright (c) 2015年 王晨辉. All rights reserved.
//

#import "IndividualViewController.h"
#import "RegistrationViewController.h"
#import "myTabBarViewController.h"
@interface IndividualViewController ()<UITextFieldDelegate>

@end

@implementation IndividualViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //NSLog(@"我的钱%@",self.identifya);
    

    //设置导航栏头文字
    self.navigationItem.title = @"填写个人信息";
    //设置导航栏背景颜色
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#ef5858" alpha:1]];
    //设置导航栏字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    //左边按钮
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
                      forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back)
     forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 20, 15);
    
    
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = menuButton;

    
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    //tableView的交互性
    _tableView.userInteractionEnabled = NO;
    [self.view addSubview:_tableView];
    
    
}
-(void)back{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
-(void)baby{
    

    [self dismissViewControllerAnimated:YES completion:nil];
    
    
    
}


#pragma mark--- tableViewDelegate 每个区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

#pragma mark--- tableViewDelegate 区尾的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        return  (SCREEN_HEIGHT-NavbarHight)/4;

        
    } else  {
        
        return  SCREEN_HEIGHT-NavbarHight-(SCREEN_HEIGHT-NavbarHight)/4+150;

        
    }
    
    
    
    
}
#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *NOTIFY = @"cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:NOTIFY];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NOTIFY];
        
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
       
        
    }

    
    if (indexPath.section == 0) {
        
        
        
        UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"老师介绍线框@2x.png.png"]];
        imageView.frame = CGRectMake(5, 5, SCREEN_WIDTH-10, (SCREEN_HEIGHT-NavbarHight)/4-10);
        [cell addSubview:imageView];
        
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, SCREEN_WIDTH/3.5, (SCREEN_HEIGHT-NavbarHight)/4-20)];
        [_imageView sd_setImageWithURL:[NSURL  URLWithString:self.pic] placeholderImage:nil];
        [imageView addSubview:_imageView];
        
        _labDown = [[UILabel alloc]initWithFrame:CGRectMake(20+SCREEN_WIDTH/3.7, (SCREEN_HEIGHT-NavbarHight)/4/3-5, SCREEN_WIDTH-30-SCREEN_WIDTH/3.7-10, 80)];
        _labDown.text = self.otherStr;
        _labDown.font = [UIFont systemFontOfSize:12];
        _labDown.textAlignment = NSTextAlignmentLeft;
        _labDown.numberOfLines = 4;
        [imageView addSubview:_labDown];
        
        
        _labTyp = [[UILabel alloc]initWithFrame:CGRectMake(20+SCREEN_WIDTH/3.7, 10, 100, 20)];
        _labTyp.text =self.name;
        _labTyp.font = [UIFont systemFontOfSize:12];
        _labTyp.textAlignment = NSTextAlignmentLeft;
        [imageView   addSubview:_labTyp];
        
    } else {
        
        _tableView.userInteractionEnabled = YES;
        
        
        
        
        
        
        _nameField = [[UITextField alloc]initWithFrame:CGRectMake(55, 12, (SCREEN_WIDTH-70), 30)];
        _nameField.borderStyle = 1;
        _nameField.layer.borderColor=UIColorWithRGBA(221, 126, 127, 1).CGColor;
        _nameField.layer.borderWidth= 1.0f;
        _nameField.text = [BabyManager shareInstance].homeName;
        
        [cell addSubview:_nameField];
        UIImageView * nameImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 14, 26, 26)];
        nameImage.image = [UIImage imageNamed:@"宝宝姓名@2x.png.png"];
        [cell addSubview:nameImage];
        
        _numberField = [[UITextField alloc]initWithFrame:CGRectMake(55, 59, (SCREEN_WIDTH-70), 30)];
        _numberField.borderStyle = 1;
        _numberField.placeholder = @"    输入联系方式";
        _numberField.layer.borderColor=UIColorWithRGBA(221, 126, 127, 1).CGColor;
        _numberField.layer.borderWidth= 1.0f;
        [cell addSubview:_numberField];
        UIImageView * numberImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 61, 26, 26)];
        numberImage.image = [UIImage imageNamed:@"联系方式@2x.png.png"];
        [cell addSubview:numberImage];
        
        _peopleField= [[UITextField alloc]initWithFrame:CGRectMake(55, 106, (SCREEN_WIDTH-70), 30)];
        _peopleField.borderStyle = 1;
        _peopleField.placeholder = @"    输入联系人";
        _peopleField.layer.borderColor=UIColorWithRGBA(221, 126, 127, 1).CGColor;
        _peopleField.layer.borderWidth= 1.0f;
        [cell addSubview:_peopleField];
        UIImageView * peopleImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 108, 26, 26)];
        peopleImage.image = [UIImage imageNamed:@"联系人@2x.png.png"];
        [cell addSubview:peopleImage];
        
        _cityField = [[UITextField alloc]initWithFrame:CGRectMake(55, 153, (SCREEN_WIDTH-70), 30)];
        _cityField.borderStyle = 1;
        _cityField.placeholder = @"    输入详细地址";
        _cityField.layer.borderColor=UIColorWithRGBA(221, 126, 127, 1).CGColor;
        _cityField.layer.borderWidth= 1.0f;
        [cell addSubview:_cityField];
        UIImageView * cityImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 155, 26, 26)];
        cityImage.image = [UIImage imageNamed:@"地址@2x.png.png"];
        [cell addSubview:cityImage];
        
        
        
        
        UILabel * payLab = [self andfrom:CGRectMake(SCREEN_WIDTH-160, _cityField.frame.origin.y+_cityField.frame.size.height+10, 70, 30) andtext:@"需支付：" andfont:[UIFont systemFontOfSize:15] andAlignment:NSTextAlignmentLeft];
//        payLab.backgroundColor = [UIColor redColor];
        [cell addSubview:payLab];
        
        
        UILabel * maneiLab = [self andfrom:CGRectMake(SCREEN_WIDTH-90, _cityField.frame.origin.y+_cityField.frame.size.height+10, 70, 30) andtext:[NSString stringWithFormat:@"%@元",self.manei] andfont:[UIFont systemFontOfSize:18] andAlignment:NSTextAlignmentLeft];
        maneiLab.textColor = [UIColor orangeColor];
//        maneiLab.backgroundColor = [UIColor blackColor];
        [cell addSubview:maneiLab];

        
        
        
        UIButton * but = [UIButton buttonWithType:1];
        but.frame = CGRectMake(70, payLab.frame.origin.y+payLab.frame.size.height+20, (SCREEN_WIDTH-140), 40);
        [but setBackgroundImage:[UIImage imageNamed:@"确认预约-点击后@2x.png.png"] forState:UIControlStateNormal];
        [but addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:but];
        
        
        _timeField.delegate = self;
        _nameField.delegate = self;
        _numberField.delegate = self;
        _peopleField.delegate = self;
        _cityField.delegate = self;
        
        
        
        
        
    }
    

    
    
    return cell;
    
    

    
    
}
- (BOOL)isChinese:(NSString *)msg{
    
    for (int i = 0; i < [msg length]; i ++) {
        
        unichar a = [msg characterAtIndex:i];
        
        if ((int)a < 255) {
            
            return NO;
        }
    }
    
    return YES;
}

- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    if (mobileNum.length != 11)
    {
        return NO;
    }
    /**
     * 手机号码:
     * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9]
     * 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178
     * 联通号段: 130,131,132,155,156,185,186,145,176
     * 电信号段: 133,153,180,181,189,177
     */
    
    
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9])\\d{8}$";
    /**
     * 中国移动：China Mobile
     * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178
     */
    NSString *CM = @"^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$";
    /**
     * 中国联通：China Unicom
     * 130,131,132,155,156,185,186,145,176
     */
    NSString *CU = @"^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$";
    /**
     * 中国电信：China Telecom
     * 133,153,180,181,189,177
     */
    NSString *CT = @"^1(33|53|77|8[019])\\d{8}$";
    
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}
/** UILabel方法 */
-(UILabel *) andfrom:(CGRect)frome andtext:(NSString *)text andfont:(UIFont *)font andAlignment:(NSTextAlignment)Alignment{
    
    UILabel * lab_tp = [[UILabel alloc]initWithFrame:frome];
    lab_tp.text = text;
    lab_tp.textAlignment = Alignment;
    lab_tp.font = font;
    lab_tp.numberOfLines = 1;
    return lab_tp;
    
}
#pragma mark--- but点击事件

-(void)btnClick{
    

    if (_numberField.text.length!=0&&_nameField.text.length!=0&&_peopleField.text.length!=0&&_cityField.text.length!=0) {
        
        
        
        if(![self isMobileNumber:_numberField.text]) {
            
            [JKAlert showMessage:@"提示" message:@"请正确输入手机号码。"];
        }else  if (![self isChinese:_peopleField.text] ) {
            ////NSLog(@"请选择地址");
            [JKAlert showMessage:@"提示" message:@"宝宝姓名只能为中文"];
            
        } else{
            
            NSUserDefaults *user2 = [NSUserDefaults standardUserDefaults];
            NSString *ID = [user2 objectForKey:@"ID"];
            
            NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
            NSString *babyID = [user1 objectForKey:@"babyID"];
            
            
            NSDictionary *param2 = @{@"identifyId": self.identifya,@"address":_cityField.text,@"babyId":[BabyManager shareInstance].babyId,@"userId":ID,@"cellNumber":_numberField.text,@"contact":_peopleField.text};
            
            
            
            [UserDAL teacherAppointmentParams:param2 withBlock:^(NSDictionary *data, NSError *error) {
                
                if (data) {
                    
                    ////NSLog(@"%@等等等等等等等",data);
                    
                    NSString * str = [data objectForKey:@"msg"];
                    
                    
                    
                    RegistrationViewController * reg = [[RegistrationViewController alloc]init];
                    reg.number = self.manei;
                    reg.name = self.name;
                    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:reg];
                    [self  presentViewController:nav animated:YES completion:nil];
                    
                    
                    
                    [JKAlert showMessage:@"提示" message:str];
                    
                    
                    
                    
                } else {
                    
                    
                    
                }
                
                
                
                
            }];
            
            

            
            
        }

        ////NSLog(@"%@,%@,%@,%@,%@",_timeField.text,_numberField.text,_nameField.text,_peopleField.text,_cityField.text);
        
        NSString *passWord = [NSString stringWithFormat:@"%@,%@,%@,%@,%@",_timeField.text,_numberField.text,_nameField.text,_peopleField.text,_cityField.text];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:passWord forKey:@"aaaaaaaa"];
        
    } else {
        [JKAlert showMessage:@"提示" message:@"请完善您的信息"];
    }
    

    

    
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [_cityField resignFirstResponder];
    
    [_timeField resignFirstResponder];
    
    [_nameField resignFirstResponder];
    
    [_peopleField resignFirstResponder];

    [_numberField resignFirstResponder];

    
    
    return YES;
    
}


#pragma mark--- tableViewDelegate 有几个区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
    
}
#pragma mark--- tableViewDelegat 点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];


    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
